// var createError = require('http-errors'); // 处理服务器错误的模块
var express = require('express'); // express核心
var path = require('path'); // path模块
var cookieParser = require('cookie-parser'); // cookie转换模块，用于设置cookie
var logger = require('morgan'); // 摩根  日志模块

//路由
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
let stuRouter = require('./routes/stu')

// 创建服务
var app = express();

// view engine setup --设置试图引擎
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev')); // 输出开发模式的日志
app.use(express.json()); //传输的是json格式
app.use(express.urlencoded({
    extended: false
})); // 处理post请求的数据
app.use(cookieParser()); // 启用cookie插件

// 静态资源托管
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.resolve("asess")))

// 匹配路由  一级路径
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/test',stuRouter)

// // catch 404 and forward to error handler
// app.use(function(req, res, next) {
//   next(createError(404));
// });

// // error handler
// app.use(function(err, req, res, next) {
//   // set locals, only providing error in development
//   res.locals.message = err.message;
//   res.locals.error = req.app.get('env') === 'development' ? err : {};

//   // render the error page
//   res.status(err.status || 500);
//   res.render('error');
// });

module.exports = app;